<?php
// vim: foldmethod=marker
/**
 *  Xanhte_ViewClass.php
 *
 *  @author     {$author}
 *  @package    Xanhte
 *  @version    $Id: Xanhte_ViewClass.php 163 2008-03-11 18:54:07Z mikhail.miguel $
 */

// {{{ Xanhte_ViewClass
/**
 *  viewクラス
 *
 *  @author     {$author}
 *  @package    Xanhte
 *  @access     public
 */
class Xanhte_ViewClass extends Ethna_ViewClass
{
	/// xoops_module_headerに入れる配列
	var $xoops_module_header = array();
	
	
	/// alias function
	function &getM($c)
	{
		  return $this->backend->getManager($c);
	}
	
	
    /**
     *  共通値を設定する
     *
     *  @access protected
     *  @param  object  Xanhte_Renderer  レンダラオブジェクト
     */
    function _setDefault(&$renderer)
    {
		$smarty =& $renderer->engine;
		
		$smarty->left_delimiter = '<{';
		$smarty->right_delimiter = '}>';
		$smarty->force_compile = true ;
		$smarty->trusted_dir = $_SERVER['DOCUMENT_ROOT'];
		
    }
	
	
	
	
    /**
     *  ビューを出力する
     *
     *  @param  bool    $capture    true ならば出力を表示せずに返す
     *  @access public
     */
	function forward($capture=false)
	{
		$this->assignCommonTemplateVars();
		// AppManagerを利用したAssgin
		/// 管理画面/Block/でテンプレの形式を変える
		/// 管理画面かどうかでテンプレの位置を変える
		if (!$this->backend->isAdminView()){
			$template_dir = $this->getPublicTemplateDir();
			$forward_path = $this->getPublicForwardPath();
		} else {
			$forward_path = $this->forward_path;
			$template_dir = $this->backend->controller->getTemplatedir();
		}
		
		// view Renderer
        $renderer =& $this->_getRenderer();
		$form_ne_array =& $this->af->getArray(false);
		$renderer->setPropByRef('form_ne', $form_ne_array);		
		
        $this->_setDefault($renderer);
		$this->_assignXoopsModuleHeader();
		
		 // ここで再定義(ダサダサ・・・しかし、Rendererとかなどなどと絡むよりは。。。)
		$renderer->setTemplateDir($template_dir);
		$renderer->engine->template_dir = $template_dir;
		
		$renderer->perform($forward_path, $capture);
//		var_dump($GLOBALS['xoopsTpl']==$renderer->engine);
//		var_dump($GLOBALS['xoopsTpl']===$renderer->engine);
//		var_dump($GLOBALS['xoopsTpl']->get_template_vars('comment_mode'));
//		var_dump($renderer->engine->get_template_vars('comment_mode'));
	}
	
	
	
	/**
	 *	XOOPSのHeaderとか出力しないで、EthnaApplicationの部分のみ出力する
	 *
	 *	@access	protected
     *  @param  bool    $capture    true ならば出力を表示せずに返す	 
	 *	@return null
	 */
	function forward_alone($capture=false)
	{
		$this->assignCommonTemplateVars();
		
//		parent::forward();
		$template_dir = $this->getPublicTemplateDir();
		$forward_path = $this->getPublicForwardPath();
		
        $renderer =& $this->_getRenderer();
		
		$form_ne_array =& $this->af->getArray(false);
		$renderer->setPropByRef('form_ne', $form_ne_array);		
        $this->_setDefault($renderer);
		
		 // ここで再定義
		$renderer->setTemplateDir($template_dir);
		$renderer->engine->template_dir = $template_dir;
		
		$renderer->perform($forward_path, $capture);
		exit();
	}
	
	
	
	/**
	 *	モジュールの表側用のテンプレートパスを取得
	 *
	 *	@access	protected
	 *	@return	string 
	 */
	function getPublicForwardPath()
	{
		$mModule =& $this->backend->getProcessModule();
		$dirname = $mModule->mXoopsModule->get('dirname');
		
		if ($this->backend->isBlockView()){
			$forward_path = sprintf('%s_%s', $dirname, str_replace('/', '_', $this->forward_path));
		} else {
			$forward_path = preg_replace('/^[a-z0-9]+/i', $dirname, $this->forward_path); // acitonの最初の段は$dirnameに変更
			$forward_path = str_replace('/', '_', $forward_path);
		}
		
		return $forward_path;
	}
	
	/**
	 *	モジュールの表側用のテンプレートディレクトリを取得
	 *
	 *	@access	protected
	 *	@return	string 
	 */
	function getPublicTemplateDir()
	{
		$mModule =& $this->backend->getProcessModule();
		$dirname = $mModule->mXoopsModule->get('dirname');
		
		$template_dir = sprintf("%s/modules/%s/templates",
								$this->backend->xcRoot->getSiteConfig('Cube', 'Root'),
								$dirname ); 
		return $template_dir;
	}
	
	/**
	 *	CSVファイルを出力する
	 *
	 *	@access	protected
	 *  @param string $text CSVの中身
	 *  @param string $filename CSVファイルの名前
	 *	@return	null
	 */
	function printCSV($text, $filename){
		if( preg_match('/firefox/i' , $_SERVER['HTTP_USER_AGENT']) ){
			header("Content-Type: application/x-csv");
		}else{
			header("Content-Type: application/vnd.ms-excel");
		}
		header("Content-Disposition: attachment ; filename=\"{$filename}\"") ;
		exit($text);
	}
	
	
	
	/// 共通的なテンプレート変数をアサイン
	function assignCommonTemplateVars()
	{
		$mModule =& $this->backend->getProcessModule();
		$xoopsModule =& $mModule->getXoopsModule();
		if ($this->backend->isBlockView()){
			$this->af->setApp('blockconf', $this->backend->getBlockOptions());
		} else {
			$gperm_m =& $this->getM('Gperm');
			$mModule =& $this->backend->getProcessModule();
			$module_admin = $gperm_m->hasModuleAdmin($mModule->mXoopsModule->mid());
			$this->af->setApp('module_admin', $module_admin);
		}
		$dirname = $mModule->mXoopsModule->get('dirname');
		$this->af->setApp('dirname', $dirname);
		$this->af->setApp('modconf', $mModule->getModuleConfig());
		$this->af->setApp('modinfo', $xoopsModule->getInfo());
		$this->af->setApp('mod', $xoopsModule->getVars());
		$this->af->setApp('now', $this->backend->time());
		$user_m =& $this->getM('Users');
		$this->af->setApp('comment_mode', $user_m->getLoginProp('umode'));
		
		// ethna config
		$config =& $this->backend->getConfig();
		$image_prop = $config->get('image_prop');
		$image_prop['upload_path'] = str_replace(XOOPS_ROOT_PATH, XOOPS_URL , $image_prop['upload_path']);
		$image_prop['thumb_path'] = str_replace(XOOPS_ROOT_PATH, XOOPS_URL, $image_prop['thumb_path']);
		$this->af->setApp('image_prop', $image_prop);
	}
	
	
	/// xoops_module_headerにCSS/JSをAssignする
	/**
	 * @param string Assignさせるcss/js(modules/dirname/以下。頭の/は無し ex. css/base.css)
	 * @retval null
	 */
	 function appendXoopsModuleHeader($filename, $dirname=null)
	 {
		 !$dirname and $dirname = $this->backend->getModuleDir();
		 !isset($this->xoops_module_header[$dirname]) and $this->xoops_module_header[$dirname] = array();
		 
		 if (!isset($this->xoops_module_header[$dirname][$filename])){
			 $this->xoops_module_header[$dirname][$filename] = true;
		 }
	 }
	
	
	/// 貯めたXoopsModuleHeaderをレンダリングする
	function _assignXoopsModuleHeader()
	{
		$xmh = '';
		foreach ($this->xoops_module_header as $dirname => $headers){
			foreach (array_keys($headers) as $header){
				if (preg_match('/.*(css|js)$/i', $header, $m)){
					$format = '';
					$href = sprintf("%s/modules/%s/%s",	XOOPS_URL, $dirname, $header);
					if (strcasecmp($m[1],'css')===0){
						$format = '<link rel="stylesheet" type="text/css" media="screen" href="%s" />';
					} else if (strcasecmp($m[1],'js')===0){
						$format = '<script type="text/javascript" src="%s"></script>';
					}
					$xmh .= sprintf($format, $href)."\n";
				}
			}
		}
		$xmh = $GLOBALS['xoopsTpl']->get_template_vars('xoops_module_header').$xmh;
		$GLOBALS['xoopsTpl']->assign('xoops_module_header', $xmh);
	}
}
// }}}
